#!/usr/bin/env python3
"""
Ford URL探索和API发现脚本
基于URL0的404发现，探索正确的Ford Focus页面和API端点
"""

import json
import time
import sys
import os
from urllib.parse import urljoin, urlparse

sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
from src.base import BaseWebDriver
from selenium.webdriver.common.by import By

class FordURLExplorer:
    """Ford URL探索器"""
    
    def __init__(self):
        self.base_driver = None
        self.driver = None
        self.results = {
            "exploration_date": time.strftime("%Y-%m-%d %H:%M:%S"),
            "original_url0": "",
            "url_tests": [],
            "working_urls": [],
            "api_discoveries": [],
            "focus_specific_findings": {}
        }
    
    def load_config(self):
        """加载配置"""
        try:
            with open('fetchNgc2.json', 'r', encoding='utf-8') as f:
                config = json.load(f)
            
            self.results["original_url0"] = config.get('sub_model', {}).get('url0', '')
            print(f"📋 原始URL0: {self.results['original_url0']}")
            return True
            
        except Exception as e:
            print(f"❌ 加载配置失败: {e}")
            return False
    
    def setup_driver(self):
        """初始化WebDriver"""
        try:
            print("🔧 初始化WebDriver...")
            self.base_driver = BaseWebDriver()
            self.base_driver.setup_driver()
            self.driver = self.base_driver.driver
            print("✅ WebDriver初始化成功")
            return True
        except Exception as e:
            print(f"❌ WebDriver初始化失败: {e}")
            return False
    
    def test_url_variants(self):
        """测试不同的URL变体"""
        print("🔍 测试Ford Focus页面的可能URL...")
        
        # 基于已知的工作URL和常见模式的URL变体
        base_urls = [
            "https://www.ford.co.uk",
            "https://www.ford.co.uk/cars",
            "https://www.ford.co.uk/vehicles"
        ]
        
        focus_paths = [
            "/focus",
            "/cars/focus",
            "/vehicles/focus",
            "/cars/model/focus",
            "/models/focus",
            "/shop/focus",
            "/shop/cars/focus",
            "/shop/build-your-ford/focus"
        ]
        
        # 组合URL进行测试
        test_urls = []
        for base in base_urls:
            for path in focus_paths:
                test_urls.append(base + path)
        
        # 添加一些已知模式
        test_urls.extend([
            "https://www.ford.co.uk/cars/focus",  # 最可能的URL
            "https://www.ford.co.uk/shop/build-your-ford/ngc2/ice2/model?model=focus",
            "https://www.ford.co.uk/cars/model#focus",  # 原始URL0
            "https://www.ford.co.uk/cars/model/focus",  # 不含#的版本
        ])
        
        working_urls = []
        
        for url in test_urls[:10]:  # 限制测试数量
            try:
                print(f"  🌐 测试: {url}")
                self.driver.get(url)
                time.sleep(3)
                
                title = self.driver.title
                final_url = self.driver.current_url
                
                # 检查是否成功加载
                is_404 = "404" in title.lower() or "not found" in title.lower()
                is_error = "error" in title.lower()
                has_focus = "focus" in title.lower() or "focus" in self.driver.page_source.lower()
                
                url_test = {
                    "test_url": url,
                    "final_url": final_url,
                    "title": title,
                    "status": "working" if not is_404 and not is_error else "error",
                    "has_focus_content": has_focus,
                    "redirected": url != final_url
                }
                
                self.results["url_tests"].append(url_test)
                
                if not is_404 and not is_error and has_focus:
                    working_urls.append(url_test)
                    print(f"    ✅ 工作正常: {title}")
                elif not is_404 and not is_error:
                    print(f"    ⚠️  页面正常但无Focus内容: {title}")
                else:
                    print(f"    ❌ 错误页面: {title}")
                    
            except Exception as e:
                print(f"    ❌ 访问失败: {e}")
                self.results["url_tests"].append({
                    "test_url": url,
                    "error": str(e),
                    "status": "failed"
                })
        
        self.results["working_urls"] = working_urls
        print(f"✅ 找到 {len(working_urls)} 个工作的Focus相关页面")
        
        return working_urls
    
    def analyze_working_pages(self):
        """分析找到的工作页面"""
        print("🔍 分析工作页面...")
        
        focus_findings = {}
        
        for url_info in self.results["working_urls"][:3]:  # 分析前3个
            try:
                print(f"  📄 分析页面: {url_info['final_url']}")
                
                self.driver.get(url_info['final_url'])
                time.sleep(5)
                
                # 查找Focus相关信息
                focus_elements = self.driver.find_elements(By.XPATH, "//*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'focus')]")
                
                focus_content = []
                for elem in focus_elements[:10]:
                    try:
                        text = elem.text.strip()
                        if text and len(text) > 5 and len(text) < 200:
                            focus_content.append({
                                "text": text,
                                "tag": elem.tag_name,
                                "href": elem.get_attribute('href')
                            })
                    except:
                        continue
                
                # 查找规格和配置相关链接
                spec_keywords = ['specification', 'configure', 'build', 'model', 'trim', 'variant']
                spec_links = []
                
                for keyword in spec_keywords:
                    try:
                        xpath = f"//*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{keyword}')]"
                        elements = self.driver.find_elements(By.XPATH, xpath)
                        
                        for elem in elements[:3]:
                            try:
                                text = elem.text.strip()
                                href = elem.get_attribute('href')
                                if text and href:
                                    spec_links.append({
                                        "text": text,
                                        "href": href,
                                        "keyword": keyword
                                    })
                            except:
                                continue
                    except:
                        continue
                
                page_analysis = {
                    "url": url_info['final_url'],
                    "title": url_info['title'],
                    "focus_content": focus_content[:5],
                    "spec_links": spec_links[:5],
                    "total_focus_mentions": len(focus_elements)
                }
                
                focus_findings[url_info['final_url']] = page_analysis
                
            except Exception as e:
                print(f"    ❌ 页面分析失败: {e}")
        
        self.results["focus_specific_findings"] = focus_findings
        return focus_findings
    
    def discover_apis_from_working_pages(self):
        """从工作页面中发现API端点"""
        print("🔍 从工作页面发现API端点...")
        
        api_discoveries = []
        
        for url_info in self.results["working_urls"][:2]:  # 检查前2个
            try:
                print(f"  🔗 检查页面API: {url_info['final_url']}")
                
                self.driver.get(url_info['final_url'])
                time.sleep(5)
                
                # 检查页面源码中的API相关内容
                page_source = self.driver.page_source
                
                # 查找可能的API端点
                import re
                
                # 查找API URL模式
                api_patterns = [
                    r'https?://[^"\']*api[^"\']*',
                    r'https?://[^"\']*service[^"\']*',
                    r'/api/[^"\']*',
                    r'servicescache\.ford\.com[^"\']*'
                ]
                
                found_apis = set()
                
                for pattern in api_patterns:
                    matches = re.findall(pattern, page_source, re.IGNORECASE)
                    for match in matches:
                        # 清理匹配结果
                        clean_match = match.strip('\'"<>(){}[]')
                        if len(clean_match) > 10:  # 过滤太短的匹配
                            found_apis.add(clean_match)
                
                if found_apis:
                    api_discoveries.append({
                        "source_page": url_info['final_url'],
                        "apis_found": list(found_apis)[:10]  # 限制数量
                    })
                    
                    print(f"    ✅ 发现 {len(found_apis)} 个潜在API端点")
                else:
                    print(f"    ⚠️  未发现明显的API端点")
                    
            except Exception as e:
                print(f"    ❌ API发现失败: {e}")
        
        self.results["api_discoveries"] = api_discoveries
        return api_discoveries
    
    def save_results(self):
        """保存探索结果"""
        try:
            output_file = "./release/ford_url_exploration.json"
            
            os.makedirs("./release", exist_ok=True)
            
            with open(output_file, 'w', encoding='utf-8') as f:
                json.dump(self.results, f, ensure_ascii=False, indent=2)
            
            print(f"💾 探索结果已保存到: {output_file}")
            return output_file
            
        except Exception as e:
            print(f"❌ 保存结果失败: {e}")
            return None
    
    def cleanup(self):
        """清理资源"""
        if self.base_driver:
            self.base_driver.cleanup()
            print("🧹 资源已清理")
    
    def run_exploration(self):
        """运行完整探索"""
        try:
            print("🚀 开始Ford URL探索...")
            print("=" * 50)
            
            # 1. 加载配置
            if not self.load_config():
                return False
            
            # 2. 初始化WebDriver
            if not self.setup_driver():
                return False
            
            # 3. 测试URL变体
            working_urls = self.test_url_variants()
            
            if working_urls:
                # 4. 分析工作页面
                self.analyze_working_pages()
                
                # 5. 发现API端点
                self.discover_apis_from_working_pages()
            
            # 6. 保存结果
            self.save_results()
            
            # 7. 打印摘要
            self.print_summary()
            
            return True
            
        except Exception as e:
            print(f"❌ 探索过程出现错误: {e}")
            return False
        finally:
            self.cleanup()
    
    def print_summary(self):
        """打印探索摘要"""
        print("\n" + "=" * 50)
        print("📊 Ford URL探索摘要")
        print("=" * 50)
        
        print(f"🎯 原始URL0: {self.results['original_url0']}")
        print(f"🔍 测试URL数量: {len(self.results['url_tests'])}")
        print(f"✅ 工作URL数量: {len(self.results['working_urls'])}")
        print(f"🔗 发现API源: {len(self.results['api_discoveries'])}")
        
        # 显示工作的URL
        if self.results['working_urls']:
            print(f"\n✅ 工作的Focus页面:")
            for i, url in enumerate(self.results['working_urls']):
                print(f"  {i+1}. {url['final_url']}")
                print(f"     标题: {url['title']}")
        
        # 显示API发现
        if self.results['api_discoveries']:
            print(f"\n🔗 发现的API端点:")
            for discovery in self.results['api_discoveries']:
                print(f"  来源: {discovery['source_page']}")
                for api in discovery['apis_found'][:3]:
                    print(f"    - {api}")

def main():
    """主函数"""
    explorer = FordURLExplorer()
    success = explorer.run_exploration()
    
    if success:
        print("\n🎉 Ford URL探索完成！")
        print("📁 请查看 ./release/ford_url_exploration.json 获取详细结果")
    else:
        print("\n❌ Ford URL探索失败")
    
    return success

if __name__ == "__main__":
    main()
